#ifndef _WEBRTC_INTERFACE_H_
#define _WEBRTC_INTERFACE_H_

#include <stdint.h>

#ifdef LIBWEBRTC_EXPORTS
#define WEBRTC_API __declspec(dllexport)
#else
#ifdef LIBWEBRTC_IMPORTS
#define WEBRTC_API __declspec(dllimport)
#else
#define WEBRTC_API extern
#endif
#endif

//class WebrtcInterface;

typedef enum _WEBRTC_CLIENT_STATUS_ {
    REG_OK,
    REG_ERR,
    UNREG_OK,
    UNREG_ERR,
    CALL_OK,
    CALL_ERR,
    CALL_HANGUP,
    DATA_STREAM_OK,
    DATA_STREAM_HANGUP_OK,
    DATA_STREAM_ERR,

    //peer
            PEER_CALLED_INCOMING,
    PEER_CALLED_HANGUP,
    PEER_DATA_STREAM_INCOMING,
    PEER_DATA_STREAM_HANGUP,

    ERR
} WebrtcClientStatus_t;

typedef struct _RTC_SIP_CONFIG_ {
    const char *local_ip;
    uint16_t local_port;
    const char *server_ip;
    uint16_t server_port;
    uint8_t trans_proto; //0-udp, 1-tcp
    const char *username;
    const char *pwd;
    int timeout_sec;
    char test_file_name[256];
}RtcSipConfig_t;

typedef enum _AUDIO_CODEC_ {
    OPUS = 0,
    G711
}AudioCodec_t;
typedef struct _RTC_AUDIO_CONFIG_ {
    AudioCodec_t codec;
} RtcAudioConfig_t;

typedef enum _VIDEO_CODEC_ {
    H264 = 0
}VideoCodec_t;

typedef struct _RTC_VIDEO_CONFIG_ {
    VideoCodec_t codec;
    int width;
    int height;
    int fps;
    int start_bitrate_kbps;
    int max_bitrate_kbps;
    int min_bitrate_kbps;
    int recv_bitrate_kbps;
} RtcVideoConfig_t;

typedef struct _RTC_DATA_CONFIG_ {
    VideoCodec_t codec;
    int width;
    int height;
    int fps;
    int min_bitrate_kbps;
    int recv_bitrate_kbps;
} RtcDataConfig_t;

typedef struct _WEBRTC_MEDIA_STATUS_ {
    AudioCodec_t audio_codec;
    VideoCodec_t video_codec;
    int recv_width;
    int recv_height;
    int send_width;
    int send_height;
    int audio_recv_bps;
    int video_recv_bps;
    int audio_send_bps;
    int video_send_bps;
    size_t send_audio_bytes;
    size_t recv_audio_bytes;
    size_t send_video_bytes;
    size_t recv_video_bytes;
    size_t audio_packets_received;
    size_t audio_packets_sent;
    size_t video_packets_received;
    size_t video_packets_sent;
    size_t recv_frames;
    size_t send_frames;
    int recv_fps;
    int send_fps;
    float audio_recv_loss;
    float video_recv_loss;
} WebrtcMediaStatus_t;

typedef struct _MEDIA_DEVICE_ {
    uint8_t typ;// 0:audio play, 1:audio mic, 2:video cap
    int id;
    char name[256];
    char guid[256];
} MediaDevice_t;

typedef struct  {
    int width;  // Number of pixels.
    int height;  // Number of pixels.
    int64_t interval;  // Nanoseconds.
    uint32_t fourcc;  // Color space. FOURCC_ANY means that any color space is OK.
} VideoFormat_t;

class WebrtcClientCallback {
public:
    virtual void *OnAllocWnd(int wnd_id = 0) = 0;//return new wnd
    virtual void OnFreeWnd(void *wnd) = 0;
    virtual void OnClientStatus(WebrtcClientStatus_t stat, const char *message) = 0;
    virtual void OnRemoteMediaConfig(RtcAudioConfig_t *a_cfg, RtcVideoConfig_t *v_cfg) = 0;
    virtual void OnLogMessage(const char* message)= 0;
protected:
    virtual ~WebrtcClientCallback() {}
};

class WebrtcVideoCapturerCallback {
public:
    typedef void(*Free) (uint8_t *ptr);
    virtual bool CapturerI420FrameAlloc(uint8_t **i420_frame, size_t *size, int *fps, int *width, int *height, Free &free_cb) = 0;

protected:
    virtual ~WebrtcVideoCapturerCallback() {}
};

class WebrtcVideoRenderCallback {
public:
    virtual void OnFrame(const uint8_t* frame, size_t width, size_t height, void *wnd) = 0;

protected:
    virtual ~WebrtcVideoRenderCallback() {}
};

const char kOffer[] = "offer";
const char kAnswer[] = "answer";
class WebrtcInterface {
public:
#ifdef WEBRTC_ANDROID
    virtual void SetVideoRenderCallback(WebrtcVideoRenderCallback *remote_render_cb, WebrtcVideoRenderCallback *local_render_cb) = 0;
#endif // !WEBRTC_ANDROID

    virtual int Login(RtcSipConfig_t &cfg) = 0;
    virtual int Logout() = 0;
    virtual void SetUserAgent (const char *user_agent) = 0;
    virtual int CreateCall(const char *peer_id, void *local_wnd, bool using_dtls, const char *type) = 0;
    virtual int RejectCall(const char *peer_id, bool reject) = 0;
    virtual int ReleaseCall() = 0;

    virtual int GetVideoFormat(VideoFormat_t *&fm) = 0;
    virtual int SetMediaConfig(RtcAudioConfig_t *a_cfg, RtcVideoConfig_t *v_cfg) = 0;
    virtual int SetDataConfig(RtcDataConfig_t *d_cfg) = 0;

    virtual int GetMediaStatus(WebrtcMediaStatus_t *stat) = 0;

    virtual MediaDevice_t* GetMediaDevices(int *count, uint8_t typ/* 0:audio play, 1:audio mic, 2:video cap*/) = 0;
    virtual bool SetMediaDevice(uint8_t typ, int dev_id) = 0;

    virtual bool StartMediaDevicePreview(uint8_t typ/* 0:audio play, 1:audio mic, 2:video cap*/, int dev_id, void *handle) = 0;
    virtual bool StopMediaDevicePreview(uint8_t typ/* 0:audio play, 1:audio mic, 2:video cap*/) = 0;

    virtual bool ResetLocalWnd(void *wnd) = 0;
    virtual bool HideVideoCapture(bool hide) = 0;
    virtual bool MicMute(bool mute) = 0;
    virtual bool VolumeControl(int volume) = 0;//volume 0~100
    virtual void UpdateRemoteWnd(void *wnd) = 0;

    virtual int StartDataStream(WebrtcVideoCapturerCallback *cb, bool initiator) = 0;
    virtual int StopDataStream() = 0;

    virtual bool StartRecord(const char* file_name) = 0;
    virtual bool StopRecord() = 0;

protected:
    virtual ~WebrtcInterface() {};
};

WEBRTC_API WebrtcInterface* CreateWebrtcInterface(WebrtcClientCallback *call_cb, WebrtcVideoCapturerCallback *capturer_cb = 0, int interface_id = 0);//capturer_cb = 0 for use default video capturer
WEBRTC_API void ReleaseWebrtcInterface(int interface_id);

WEBRTC_API int ConvertABGRToI420(const uint8_t* src_frame,
                                 size_t src_size,
                                 int src_width,
                                 int src_height,
                                 uint8_t** dst_I420frame,
                                 size_t *dst_size,WebrtcVideoCapturerCallback::Free &);

#endif //_WEBRTC_INTERFACE_H_